﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
#if UNITY_EDITOR_OSX
using UnityEditor.iOS.Xcode;
#endif
using Beyond;

public class XcodeProjectProcessor {

    public static void update() {
#if UNITY_EDITOR_OSX
        Debug.Log("【更新XcodeProject】");
        string projPath = Application.dataPath;
        projPath = projPath.Substring(0, projPath.LastIndexOf("/")) + "/proj.ios";
        projPath = PBXProject.GetPBXProjectPath(projPath);
        Debug.Log ("  xcProj Path: " + projPath);
        if (projPath == null || projPath.Length == 0) {
            return;
        }

        string projText = File.ReadAllText (projPath);
        if (projText.Contains("libz.tbd")) {
            Debug.Log ("  xcProj 已是最新");
            return;
        }
        PBXProject proj = new PBXProject();
        proj.ReadFromString(projText);
        string targetGuid = proj.TargetGuidByName(PBXProject.GetUnityTargetName());
        Debug.Log ("  xcProj target guid = " + targetGuid);
        string[] libs = new string[]{ "libz.tbd", "libresolv.tbd", "libsqlite3.0.tbd", "libxml2.tbd" };
        foreach (string s in libs) {
            proj.AddFileToBuild (targetGuid, proj.AddFile ("usr/lib/" + s, "Frameworks/" + s, PBXSourceTree.Sdk));
        }
        string[] frameworks = new string[]{ "Accelerate.framework", "AdSupport.framework", "GLKit.framework", "WebKit.framework", "MessageUI.framework", 
            "SafariServices.framework", "MobileCoreServices.framework", "CoreTelephony.framework", "Photos.framework", "PhotosUI.framework" };
        foreach (string s in frameworks) {
            proj.AddFrameworkToProject (targetGuid, s, false);
        }
        File.WriteAllText(projPath, proj.WriteToString());
#endif
    }

}